<template>
  <view class="login-page">
    <view class="logo">
      <view class="icon">🌊</view>
      <text class="title">鸣潮 · 溢彩画</text>
      <text class="subtitle">请登录开始游戏</text>
    </view>
    
    <view class="form-container">
      <view class="form-item">
        <text class="label">用户名</text>
        <input 
          class="input" 
          placeholder="请输入用户名"
          v-model="username"
          @input="clearError"
        />
      </view>
      
      <view class="form-item">
        <text class="label">密码</text>
        <input 
          class="input" 
          type="password"
          placeholder="请输入密码"
          v-model="password"
          @input="clearError"
        />
      </view>
      
      <text class="error-message" v-if="errorMessage">{{ errorMessage }}</text>
      
      <button class="login-btn btn" @click="handleLogin">
        登录
      </button>
      
      <button class="register-btn" @click="navigateToRegister">
        注册账号
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()
const username = ref('yang')
const password = ref('123456')
const errorMessage = ref('')

const clearError = () => {
  errorMessage.value = ''
}

const handleLogin = async () => {
  if (!username.value.trim() || !password.value.trim()) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  
  const success = await userStore.login(username.value , password.value )
  if (success) {
    // 登录成功，跳转到首页
    uni.switchTab({ url: '/pages/home/home' })
  } else {
    errorMessage.value = '用户名或密码错误'
  }
}

const navigateToRegister = () => {
  uni.navigateTo({ url: '/pages/register/register' })
}
</script>

<style scoped>
.login-page {
  background-color: #1a1a2e;
  min-height: 100vh;
  padding: 48rpx 24rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo {
  text-align: center;
  margin-bottom: 64rpx;
}

.icon {
  font-size: 96rpx;
  margin-bottom: 16rpx;
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  background-image: linear-gradient(to right, #6C5CE7, #00D2D3);
  display: block;
  margin-bottom: 8rpx;
}

.subtitle {
  font-size: 28rpx;
  color: #888;
}

.form-container {
  width: 100%;
  max-width: 500rpx;
}

.form-item {
  margin-bottom: 24rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #ccc;
  margin-bottom: 8rpx;
}

.input {
  width: 100%;
  padding: 20rpx;
  background-color: #2d3436;
  border: 1px solid #444;
  border-radius: 8rpx;
  color: #fff;
  font-size: 28rpx;
}

.error-message {
  color: #e74c3c;
  font-size: 24rpx;
  margin-bottom: 16rpx;
  display: block;
  height: 24rpx;
}

.login-btn {
  width: 100%;
  background-color: #6C5CE7;
  color: #fff;
  padding: 20rpx;
  border-radius: 8rpx;
  font-size: 32rpx;
  margin-bottom: 16rpx;
}

.register-btn {
  width: 100%;
  background-color: #333;
  color: #fff;
  padding: 20rpx;
  border-radius: 8rpx;
  font-size: 32rpx;
}
</style>